package com.vote.services.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.vote.dao.VoteUserDAO;
import com.vote.domain.VoteUser;
import com.vote.model.User;
import com.vote.services.UserService;
import com.vote.utils.ModelTransform;

/**
 * UserService实现类
 * 实现了User相关的逻辑
 * 
 * @author zhou
 *
 */
public class UserServiceImpl implements UserService {

    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(UserServiceImpl.class);

    @Autowired
    private VoteUserDAO voteUserDAO;
    
    @Override
    public boolean checkUserEmail(String userEmail) {
        String ue = voteUserDAO.getUserNameByUserEmail(userEmail);
        if(ue != null && !"".equalsIgnoreCase(ue))
            return false;
        else
            return true;
    }

    @Override
    public boolean checkUserName(String userName) {
        String un = voteUserDAO.getUserNameByUserName(userName);
        if(un != null && !"".equalsIgnoreCase(un))
            return false;
        else
            return true;
    }

    @Override
    public User getUserById(int userId) {
        VoteUser voteuser = voteUserDAO.getUser(userId);
        if(voteuser == null){
            logger.info("get user by id fail when id = " + userId + "");
            return null;
        }
        User user = ModelTransform.domain2model(voteuser);
        return user;
    }

    @Override
    public int insertUser(User user) throws Exception{
        int id;
        VoteUser voteuser = ModelTransform.model2domain(user);
        try{
            id = synInsert(voteuser);
        }
        catch (Exception e){
            logger.info("insert new user false!", e);
            return -1;
        }
        return id;
    }

    @Override
    public User login(User user) {
        
        VoteUser voteUser = new VoteUser();
        voteUser.setUserName(user.getUserName());
        voteUser.setUserPassword(user.getUserPassword());
        
        voteUser = voteUserDAO.login(voteUser);
        
        if(voteUser != null){
            User tUser = ModelTransform.domain2model(voteUser);
            return tUser;
        }
        else{
            return null;
        }
    }

    @Override
    public boolean updateEmail(User user, String email) {
        try{
        VoteUser voteUser = voteUserDAO.getUser(user.getUserId());
        voteUser.setUserEmail(email);
        voteUserDAO.updateUser(voteUser);
        }
        catch (Exception e){
            logger.info("update user email false!", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean updatePassword(User user, String password) {
        try{
        VoteUser voteUser = voteUserDAO.getUser(user.getUserId());
        voteUser.setUserEmail(password);
        voteUserDAO.updateUser(voteUser);
        }
        catch (Exception e){
            logger.info("update user password false!", e);
            return false;
        }
        return false;
    }
    
    private synchronized int synInsert(VoteUser vu){
        int id = -1;
        voteUserDAO.insertUser(vu);
        id = voteUserDAO.getLastInsertId();
        return id;
    }
}
